package thread.waitAndNotify;

//@RestController
public class WaitAndNotify {
    private final String[] shareObj = {"true"};

    public static void main(String[] args) {
        WaitAndNotify test = new WaitAndNotify();
        ThreadWait threadWait1 = test.new ThreadWait("wait thread1");
        ThreadWait threadWait2 = test.new ThreadWait("wait thread2");
        ThreadWait threadWait3 = test.new ThreadWait("wait thread3");
        ThreadNotify threadNotify = test.new ThreadNotify("notify thread");

        threadNotify.start();
        threadWait1.start();
        threadWait2.start();
        threadWait3.start();
    }

    class ThreadWait extends Thread {
        public ThreadWait(String name) {
            super(name);
        }

        public void run() {
            synchronized (shareObj) {
                while ("true".equals(shareObj[0])) {
                    try {
                        shareObj.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    class ThreadNotify extends Thread {
        public ThreadNotify(String name) {
            super(name);
        }

        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (shareObj) {
                shareObj[0] = "false";
                shareObj.notifyAll();
            }
        }
    }
}
